package pme;

import junit.framework.JUnit4TestAdapter;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class Tests {

	@Test
	public void excelTest() {
		Entry e0 = new Entry(
				"iiiiiiiiiittttttttttiiiiiiiiiittttttttttiiiiiiiiiittttttttttiiiiiiiiii",
				new Max(new NullValue(), new ConstantValue(5)));
		Entry e1 = new Entry("b", new ConstantValue(24));
		Entry e2 = new Entry("c", new NullValue());
		Entry[] testEntry = { e0, e1, e2 };
		PoorMansExcel myExcel = new PoorMansExcel(testEntry);

		assertEquals(
				  		  "  1 iiiiiiiiiittttttttttiiiiiiiiiittttttttttiiiiiiiiiitttttttttt 5.0\n"
						+ "  2 b                                                            24.0\n"
						+ "  3 c                                                            0.0\n",
				myExcel.toString());

		myExcel.resize(4);
		assertEquals(4, myExcel.getSize());
		

		myExcel.update(4, e2);

		assertEquals(
		  		  "  1 iiiiiiiiiittttttttttiiiiiiiiiittttttttttiiiiiiiiiitttttttttt 5.0\n"
				+ "  2 b                                                            24.0\n"
				+ "  3 c                                                            0.0\n"
				+ "  4 c                                                            0.0\n",
				myExcel.toString());

		myExcel.update(2, e0);
		assertEquals(
		  		  "  1 iiiiiiiiiittttttttttiiiiiiiiiittttttttttiiiiiiiiiitttttttttt 5.0\n"
				+ "  2 iiiiiiiiiittttttttttiiiiiiiiiittttttttttiiiiiiiiiitttttttttt 5.0\n"
				+ "  3 c                                                            0.0\n"
				+ "  4 c                                                            0.0\n",
				myExcel.toString());

		assertEquals(true, myExcel.update(2, e1));
		assertEquals(false, myExcel.update(5, e1));
		
		myExcel.resize(0);
		assertEquals(4, myExcel.getSize());
	}

	@Test
	public void AddMax() {
		Value testVal = new Add(new Multiply(new ConstantValue(2),
				new ConstantValue(3)), new Max(new ConstantValue(0.00),
				new ConstantValue(5)));

		assertEquals(11, testVal.evaluate(), 0.001);

	}

	@Test
	public void Div0() {
		Value testVal = new Divide(new Multiply(new ConstantValue(2),
				new ConstantValue(3)), new Min(new NullValue(),
				new ConstantValue(5)));

		assertEquals(0, testVal.evaluate(), 0.001);
	}

	@Test
	public void allCalc() {
		Value testVal = new Add(new Average(new Max(new ConstantValue(1),
				new ConstantValue(2)), new Multiply(new ConstantValue(3),
				new ConstantValue(4))), new Divide(new Subtract(
				new ConstantValue(5), new ConstantValue(6)), new Min(
				new ConstantValue(7), new ConstantValue(8))));

		assertEquals(6.857, testVal.evaluate(), 0.001);
	}

	@Test
	public void nullTests() {
		Value testVal = new Average(null, null);
		assertEquals(0, testVal.evaluate(), 0.001);

		testVal = new Average(new ConstantValue(6), null);
		assertEquals(3, testVal.evaluate(), 0.001);

		testVal = new Average(null, new ConstantValue(8));
		assertEquals(4, testVal.evaluate(), 0.001);

		testVal = new Add(null, null);
		assertEquals(0, testVal.evaluate(), 0.001);

		testVal = new Max(null, null);
		assertEquals(0, testVal.evaluate(), 0.001);

		testVal = new Min(null, new ConstantValue(4));
		assertEquals(0, testVal.evaluate(), 0.001);

		testVal = new Divide(new ConstantValue(4), null);
		assertEquals(0, testVal.evaluate(), 0.001);

		testVal = new Subtract(null, new ConstantValue(-5.7));
		assertEquals(5.7, testVal.evaluate(), 0.001);

	}
}